﻿using ReflectionStudio.Core.Events;
using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using WPFExcelReport.Helpers;
using System.Collections;
using System.Collections.Generic;
namespace WPFExcelReport
{
    public partial class TEK02
    {
        public string FROMDATE = "", ENDDATE = "", FKRS = "", FLFD = "", ART = "";
        #region ==dwWBH===========
        public static readonly DependencyProperty dwGABProperty = DependencyProperty.Register("dwGAB",
            typeof(DataRow), typeof(TEK02),
            new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OndwGABChanged));
        public DataRow dwGAB
        {
            get
            {
                return (DataRow)GetValue(dwGABProperty);
            }
            set
            {
                SetValue(dwGABProperty, value);
            }
        }
        private static void OndwGABChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TEK02;
            if (src != null)
            {
                src.dwGAB = (DataRow)e.NewValue;
            }
        }
        #endregion
        ITXEXRPT ITXEXRPT;
        public TEK02()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VEKPOS();
            this.DataContext = MV;
            InitializeComponent();

        }
      

        private void txtART_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key != System.Windows.Input.Key.Enter) return;
            QueryCommand(null, null);

        }

        public override void QueryCommand(object sender, System.Windows.RoutedEventArgs e)
        {

            DataSet Result;
            DataSet Return = new DataSet();
            DataRow dwData = null;
            FKRS = txtFKRS.Text.ToString().Trim();
            FLFD = Helpers.Helper.Right("000000"+txtFLFD.Text.ToString().Trim(),6);
            ART = txtSCHNO.Text.ToString().Trim();
            if (FROMDATE == "")
            {
                FROMDATE = "20000000";
            }
            if (ENDDATE == "")
            {
                ENDDATE = "99999999";
            }
            if (FKRS == "")
            {
                FKRS = "AA";
            }
            if (FLFD == "")
            {
                FLFD = "000000";
            }

            dwData = MV.SetProd.Tables["EKPOS"].NewRow();
            dwData["EKPAUFKRS"] = FKRS;
            dwData["EKPAUFLFD"] = FLFD;
            dwData["EKPKNTRNRL"] = ART;
            dwData["EKPAEND"] = FROMDATE;
            dwData["EKPNEUD"] = ENDDATE;
            try
            {
                Result = ((VEKPOS)MV).QueryEKPOS(dwData, 7);
                MergeData(Result, true, true);

            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }

        }

        private void FROM_DATE_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                FROMDATE = "";
                FROMDATE = Helpers.Helper.GetDate(sender);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }

        }



        private void PrintCommand(object sender, RoutedEventArgs e)
        {
            try
            {
                if (ITXEXRPT == null)
                {
                    ITXEXRPT = new ITXEXRPT();
                    ITXEXRPT.Root = "C:\\IntexReport";
                    ITXEXRPT.strCompany = app.company;
                    ITXEXRPT.PrintType = "OpenExcel";
                }
                ITXEXRPT.dsSource = new DataSet();
                ITXEXRPT.dsSource.Merge(MV.SetProd);
                //ITXEXRPT.dsSource.Merge(Se.Env);

                ITXEXRPT.DocType = "JLTEK02";

                ITXEXRPT.strExcelFile = @"JLT\JLTEK02.xls";
                string filename = ITXEXRPT.WriteExcelReport();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void END_DATE_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                ENDDATE = "";
                ENDDATE = Helpers.Helper.GetDate(sender);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }

        }

    }
}
